﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyThuVien;
using QuanLyThuVien.VO;
using QuanLyThuVien.BUS;

namespace QuanLyThuVien
{
    public partial class frmTimKiemMuonTra : Form
    {
        public frmTimKiemMuonTra()
        {
            InitializeComponent();
        }

        private void btnHoanThanhSua_Click(object sender, EventArgs e)
        {
            if (dtgvThongTinMuonTra.Rows == null)
            {
                MessageBox.Show("Chua Chon Doi tuong Thong Tin Muon Tra De Sua!");
                return;
            }

            foreach (DataGridViewRow row in dtgvThongTinMuonTra.SelectedRows)
            {
                MuonTra MuonTra = new MuonTra(row.Cells[4].Value.ToString(), row.Cells[0].Value.ToString(), 
                    DateTime.Now, dtpkNgayGiaHanTra.Value);
                busMuonTra busMuonTra = new busMuonTra();
                busMuonTra.SuaMuonTra(MuonTra);
            }
            

            frmTimKiemMuonTra_Load(sender, e);
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            frmTimKiemMuonTra_Load(sender, e);
        }

        private void frmTimKiemMuonTra_Load(object sender, EventArgs e)
        {
            busMuonTra busMuonTra = new busMuonTra();
            DocGia DocGia = new DocGia(txtMaDocGia.Text, txtTenDocGia.Text);
            Sach Sach = new Sach(txtMaSach.Text, txtTenSach.Text, txtTacGia.Text, txtNhaXuatBan.Text);
            dtgvThongTinMuonTra.DataSource = busMuonTra.TimMuonTra(Sach, DocGia, dtpkNgayMuonMin.Value, dtpkNgayMuonMax.Value);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgvThongTinMuonTra.Rows == null)
            {
                MessageBox.Show("Chua Chon Doi tuong Thong Tin Muon Tra De Xoa!");
                return;
            }

            foreach (DataGridViewRow row in dtgvThongTinMuonTra.SelectedRows)
            {
                MuonTra MuonTra = new MuonTra(row.Cells[4].Value.ToString(), row.Cells[0].Value.ToString(),
                    DateTime.Now, dtpkNgayGiaHanTra.Value);
                busMuonTra busMuonTra = new busMuonTra();
                busMuonTra.XoaMuonTra(MuonTra);
            }


            frmTimKiemMuonTra_Load(sender, e);
        }

        private void dtgvThongTinMuonTra_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvThongTinMuonTra.Rows == null)
            {
                return;
            }

            dtpkNgayGiaHanTra.Value = DateTime.Parse(dtgvThongTinMuonTra.Rows[e.RowIndex].Cells[7].Value.ToString());
        }

    }
}
